package com.study.talk.module.netty.handler;

import com.study.talk.common.util.SessionUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 活跃时长消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class IMIdleStateHandler extends IdleStateHandler {

    private static final int READER_IDLE_TIME = 30;

    public IMIdleStateHandler() {
        super(READER_IDLE_TIME, 0, 0, TimeUnit.SECONDS);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        log.info("{}秒内没有收到消息，关闭连接",READER_IDLE_TIME);
        SessionUtil.unbindSession(ctx.channel());
        ctx.channel().close();
    }
}
